<template>
  <div id="homeView">
    <el-card shadow="always">

      <el-form :inline="true" size="medium" :model="userSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="userSearch.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="userSearch.nickName" placeholder="用户昵称"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="userSearch.userLevel" placeholder="会员等级"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  plain @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="edituser" icon="el-icon-plus">添加用户</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" icon="el-icon-delete" plain @click="deleteuser" >批量删除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"   plain @click="login">进入订单管理</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :data="userList"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="用户编号"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="用户名"
            align="center"
            prop="username">
        </el-table-column>
        <el-table-column
            label="密码"
            align="center"
            prop="password">
        </el-table-column>
        <el-table-column
            label="注册手机号"
            align="center"
            prop="phone">
        </el-table-column>
        <el-table-column
            label="注册邮箱"
            align="center"
            prop="email">
        </el-table-column>
        <el-table-column
            label="会员来源"
            align="center"
            prop="sourceType">
        </el-table-column>
        <el-table-column
            label="真实姓名"
            align="center"
            prop="name">
        </el-table-column>
        <el-table-column
          label="使用状态（Y正常 N非正常）"
          align="center"
          prop="status">
      </el-table-column>
        <el-table-column
            label="头像地址"
            align="center"
            prop="headPic">
        </el-table-column>
        <el-table-column
            label="qq号码"
            align="center"
            prop="qq">
        </el-table-column>
        <el-table-column
            label="账户余额"
            align="center"
            prop="accountBalance">
        </el-table-column>
        <el-table-column
            label="手机是否验证 （0否  1是）"
            align="center"
            prop="isMobileCheck">
        </el-table-column>
        <el-table-column
            label="邮箱是否检测（0否  1是）"
            align="center"
            prop="isEmailCheck">
        </el-table-column>
        <el-table-column
            label="性别，1男，2女"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="会员等级"
            align="center"
            prop="userLevel">
        </el-table-column>
        <el-table-column
            label="积分"
            align="center"
            prop="points">
        </el-table-column>
        <el-table-column
            label="经验值"
            align="center"
            prop="experienceValue">
        </el-table-column>
        <el-table-column
            label="生日"
            align="center"
            prop="birthday">
        </el-table-column>


        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteuser(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>

    </el-card>

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">

      <el-form :model="user" :rules="userrules" ref="ruleForm">
        <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户性别" prop="sex" :label-width="formLabelWidth">
          <el-radio v-model="user.sex" label="男">男</el-radio>
          <el-radio v-model="user.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册手机号" prop="phone" :label-width="formLabelWidth">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="user.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员来源：" prop="sourceType" :label-width="formLabelWidth">
          <el-input v-model="user.sourceType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName" :label-width="formLabelWidth">
          <el-input v-model="user.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="user.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="使用状态" prop="status" :label-width="formLabelWidth">
          <el-input v-model="user.status" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像地址" prop="headPic" :label-width="formLabelWidth">
          <el-input v-model="user.headPic" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="QQ号码" prop="qq" :label-width="formLabelWidth">
          <el-input v-model="user.qq" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="账户余额" prop="accountBalance" :label-width="formLabelWidth">
          <el-input v-model="user.accountBalance" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机是否验证" prop="isMobileCheck" :label-width="formLabelWidth">
          <el-input v-model="user.isMobileCheck" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱是否检测" prop="isEmailCheck" :label-width="formLabelWidth">
          <el-input v-model="user.isEmailCheck" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员等级" prop="userLevel" :label-width="formLabelWidth">
          <el-input v-model="user.userLevel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="积分" prop="points" :label-width="formLabelWidth">
          <el-input v-model="user.points" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="经验值" prop="experienceValue" :label-width="formLabelWidth">
          <el-input v-model="user.experienceValue" autocomplete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveuser">确 定</el-button>
      </div>
    </el-dialog>




  </div>
</template>

<script>
import userApi from "@/api/user"

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (value===''||value.length==0) {
        return callback(new Error('年龄不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback();
        } else {
          if (value > 200|| value<1) {
            callback(new Error('年龄在1岁到200岁之间'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    return {
      userList: [],
      page:1,
      pageSize:5,
      total:100,
      userSearch:{},


      dialogFormVisible:false,
      sids:[],
      multipleSelection: [],
      formLabelWidth:'100px',
      user: {},
      userrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },

        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },

        ],
        sex: [
          { required: true, message: '请选择员工性别', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入注册手机号', trigger: 'blur'  }
        ],
        email: [
          { required: true, message: '请输入注册邮箱', trigger: 'blur'  }
        ],
        sourceType: [
          { required: true, message: '请输入会员来源：1:PC，2：H5，3：Android，4：IOS，5：WeChat', trigger: 'blur'  }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur'  }
        ],
        name: [
          { required: true, message: '请输入真实姓名', trigger: 'blur'  }
        ],
        status: [
          { required: true, message: '请输入使用状态（Y正常 N非正常）', trigger: 'blur'  }
        ],
        headPic: [
          { required: true, message: '请输入头像地址', trigger: 'blur'  }
        ],
        qq: [
          { required: true, message: '请输入QQ号码', trigger: 'blur'  }
        ],
        accountBalance: [
          { required: true, message: '请输入账户余额', trigger: 'blur'  }
        ],
        isMobileCheck: [
          { required: true, message: '请输入手机是否验证 （0否  1是）', trigger: 'blur'  }
        ],
        isEmailCheck: [
          { required: true, message: '请输入 邮箱是否检测（0否  1是）', trigger: 'blur'  }
        ],
        userLevel: [
          { required: true, message: '请输入会员等级', trigger: 'blur'  }
        ],

        experienceValue: [
          { required: true, message: '请输入经验值', trigger: 'blur'  }
        ]



      },
      title: '添加用户',

    }
  },
  created() {


    this.search1()

  },

  methods: {



    async search(){
      const {data:res} = await userApi.search(1,this.pageSize,this.userSearch)
      console.log(res)
      if (res.code === 200) {
        this.userList = res.data.pr.rows
        this.total = res.data.pr.total
      }
      this.page=1
    },
    async search1(){
      const {data:res} = await userApi.search(this.page,this.pageSize,this.userSearch)
      console.log(res)
      if (res.code === 200) {
        this.userList = res.data.pr.rows
        this.total = res.data.pr.total
      }
    },

    edituser(){
      this.title='添加用户';
      this.user={};
      this.dialogFormVisible = true
    },

    async saveuser(){
      this.$refs.ruleForm.validate(async valid => {
        let url = "/user/save"
        let method = "post"
        if(this.user.id){
          url = "/user/update"
          method = "put"}


        if (valid) {
          const {data:res} = await userApi.save(url,method,this.user)

          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: res.message,
              duration:1000,
              onClose: () => {

                this.dialogFormVisible = false

                this.search1()
              }
            })
          }else{
            this.$message({
              type: 'error',
              message: res.message,
              duration:1000
            })
          }

          console.log('验证成功');
        } else {
          console.log('验证失败');
          return false;
        }
      });

    },
    async deleteuser(index, row){
      this.sids = []
      if(row){
        this.sids.push(row.id)
      }else{
        this.sids = this.multipleSelection.map(m => m.id)
      }
      if(this.sids.length==0)
      {
        this.$message({
          type: 'warning',
          message: '没有选择要删除的用户',
          duration: 1000
        });
        return;

      }
      this.$confirm(`是否真的要删除用户编号为${this.sids}的用户吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let _this = this
        userApi.delete(this.sids)
            .then(({data:res}) => {
              console.log("res:",res)
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: res.message,
                  duration:1000,
                  onClose: () => {

                    _this.search1()
                  }
                })
              } else {
                this.$message({
                  type: 'error',
                  message: res.message,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleEdit(index, row) {

      this.title='修改用户信息';
      this.user=row;

      this.dialogFormVisible=true;


    },
    login(){
      this.$router.push('/aboutview');
    },


    // 每页大小改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;

      this.search1()
    },
    // 当前页改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;

      this.search1()
    },
    // 选择学生改变
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
}
</script>
<style>
.el-card{
  width: 90%;
//margin: 0 auto;
}
#app{
  display: flex;
  justify-content: center;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
</style>
